import { useEffect, useState } from "react";
import { Button, Text, View } from "react-native";

export default function APP(){
   const [show,setShow]=useState(false);
   const [num,setNum]=useState(0);
   const add=()=>{
    setNum(num+1)
   }
  return(
    <View>
      <Button title="显示" onPress={() => setShow(true)}/>
      <Button title="隐藏" onPress={() => setShow(false)}/>
      <Button title="更新" onPress={add}/>
      { show && <Child num={num} /> }
    
    </View>
  )
}
// 子组件
function Child({num}:{num:number}){
  useEffect(() => {
    console.log('1：useEffect');
    // 组件卸载时执行
    return () => {
      console.log('2：子组件卸载了');
    }
  }, 
  //组件更新时触发，组件挂载时执行一次，卸载时执行一次：先执行2，在执行1
  [num])
  return (
    <View>
      <Text>子组件</Text>
      <Text>{num.toString()}</Text>
    </View>
  )
}